package com.jaydenxiao.common.commonwidget;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.databinding.BindingAdapter;
import android.databinding.BindingMethod;
import android.databinding.BindingMethods;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

/**
 * Created by hasee on 2017/10/11.
 */


@BindingMethods({
        @BindingMethod(type = TextView.class, attribute = "background", method = "setBackground"),
        @BindingMethod(type = TextView.class, attribute = "backgroundTint", method = "setBackgroundTint"),
        @BindingMethod(type = SwitchMultiButton.class, attribute = "switchBtText", method = "setSwitchBtText"),
        @BindingMethod(type = TextView.class, attribute = "visible", method = "setVisible"),
        @BindingMethod(type = TextView.class, attribute = "drawableLeft", method = "setDrawableLeft"),
        @BindingMethod(type = TextView.class, attribute = "drawableTop", method = "setDrawableTop"),
        @BindingMethod(type = TextView.class, attribute = "drawableRight", method = "setDrawableRight"),
        @BindingMethod(type = TextView.class, attribute = "drawableBottom", method = "setDrawableBottom"),
        @BindingMethod(type = TextView.class, attribute = "rotation", method = "rotationExpandIcon"),
        @BindingMethod(type = TextView.class, attribute = "margin_left", method = "setMarginLeft"),
})

public class ViewAttrAdapter {

    @BindingAdapter("android:src")
    public static void setSrc(ImageView imageView, Bitmap bitmap) {
        imageView.setImageBitmap(bitmap);
    }

    @BindingAdapter("src")
    public static void setSrc(ImageView imageView, int resId) {
        if (resId!=-1&&resId!=0){
            imageView.setImageDrawable(ContextCompat.getDrawable(imageView.getContext(),resId));
        }
    }

    @BindingAdapter("background")
    public static void setBackground(TextView textView, int resId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            textView.setBackground(ContextCompat.getDrawable(textView.getContext(), resId));
        }
    }

    @BindingAdapter("switchBtText")
    public static void setSwitchBtText(SwitchMultiButton multiButton, String[] textArray) {
        multiButton.setText(textArray);
    }

    @BindingAdapter("visible")
    public static void setVisible(ImageView view, int isVisible) {
        view.setVisibility(isVisible);
    }

    @BindingAdapter("background")
    public static void setBackground(View relativeLayout, int resId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            relativeLayout.setBackground(ContextCompat.getDrawable(relativeLayout.getContext(), resId));
        }
    }


    @BindingAdapter("backgroundTint")
    public static void setBackgroundTint(Button button, int resId) {
        button.setBackgroundColor(ContextCompat.getColor(button.getContext(), resId));
    }

    @BindingAdapter("margin_left")
    public static void setMarginLeft(View view, int margin) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        ViewGroup.MarginLayoutParams marginParams = null;
        //获取view的margin设置参数
        if (params instanceof ViewGroup.MarginLayoutParams) {
            marginParams = (ViewGroup.MarginLayoutParams) params;
        } else {
            //不存在时创建一个新的参数
            //基于View本身原有的布局参数对象
            marginParams = new ViewGroup.MarginLayoutParams(params);
        }
        marginParams.setMargins(margin,10,10,10);
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @BindingAdapter("rotation")
    public static void rotationExpandIcon(final ImageView imageView,boolean isExpand) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ValueAnimator valueAnimator = null;
            if (isExpand) {
                valueAnimator = ValueAnimator.ofFloat(0, 90);
            } else {
                valueAnimator = ValueAnimator.ofFloat(90, 0);
            }
            valueAnimator.setDuration(500);
            valueAnimator.setInterpolator(new DecelerateInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    imageView.setRotation((Float) valueAnimator.getAnimatedValue());
                }
            });
            valueAnimator.start();
        }
    }


    @BindingAdapter("drawableLeft")
    public static void setDrawableLeft(TextView textView, int resId) {
        Drawable drawable = ContextCompat.getDrawable(textView.getContext(), resId);
        textView.setCompoundDrawables(drawable, null, null, null);
    }

    @BindingAdapter("drawableTop")
    public static void setDrawableTop(TextView textView, int resId) {
        Drawable drawable = ContextCompat.getDrawable(textView.getContext(), resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, drawable, null, null);
    }

    @BindingAdapter("drawableRight")
    public static void setDrawableRight(TextView textView, int resId) {
        if (resId<=0){
            return;
        }
        Drawable drawable = ContextCompat.getDrawable(textView.getContext(), resId);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    @BindingAdapter("drawableBottom")
    public static void setDrawableBottom(TextView textView, int resId) {
        Drawable drawable = ContextCompat.getDrawable(textView.getContext(), resId);
        textView.setCompoundDrawables(null, null, null, drawable);
    }

    @BindingAdapter({"imgUrl", "placeHolderId", "errorHolderId"})
    public static void loadImage(ImageView imageView, String imgUrl, int placeHolderId,
                                 int errorHolderId) {
        Glide.with(imageView.getContext())
                .load(imgUrl)
                .placeholder(ContextCompat.getDrawable(imageView.getContext(), placeHolderId))
                .error(ContextCompat.getDrawable(imageView.getContext(), errorHolderId))
                .into(imageView);
    }

    @BindingAdapter({"imgUrl"})
    public static void loadImage(ImageView imageView, String imgUrl) {
        Glide.with(imageView.getContext())
                .load(imgUrl)
                .into(imageView);
    }


//    @BindingAdapter("ctvBackground")
//    public static void setCtvBackground(ColorTextView textView,int colorId){
//        textView.setCtvBackgroundColor(ContextCompat.getColor(textView.getContext(),colorId));
//    }

}
